package com.kehutong.charge.admin;

import com.kehutong.charge.entity.ChargeFeeChange;
import com.kehutong.charge.service.ChargeFeeChangeService;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONArray;
import org.coraframework.json.JSONObject;
import org.coraframework.logger.Logger;
import org.coraframework.logger.LoggerFactory;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.orm.Pool;


/**
 * @program: kht
 * @description: 费用信息控制类
 * @author: joneron
 * @create: 2021/6/23
 **/
@MvcAction
@ReqMapping("/admin/pay/charge/fee/info")
public class ChargeFeeChangeController {

    private static final Logger logger = LoggerFactory.getLogger(ChargeFeeChangeController.class);

    @Inject
    ChargeFeeChangeService chargeFeeChangeService;

    @ReqMapping("/page")
    //@HasPermission("pay:charge:fee:info:view")
    public Object page(JSONObject jsonObject) throws Exception {
        return chargeFeeChangeService.findPage(jsonObject);
    }

    @ReqMapping("/list")
    //@HasPermission("pay:charge:fee:info:view")
    public Object list(JSONObject jsonObject) {
        return chargeFeeChangeService.findList(jsonObject);
    }

    @Bean(newInstance=true, copy=true)
    //@HasPermission("pay:charge:fee:info:edit")
    @ReqMapping("/save")
    public Object save(Token token,JSONObject jsonObject) throws Exception {

        JSONArray itemList = jsonObject.getJSONArray("itemList");
        for (int i = 0; i < itemList.size(); i++) {
            JSONObject json = itemList.getJSONObject(i);
            ChargeFeeChange entity = Pool.newInstance(ChargeFeeChange.class);
            entity.setServiceNo(json.getString("serviceNo"));
            entity.setChargeBillNo(json.getString("chargeBillNo"));
            entity.setBillAmount(json.getBigDecimal("billAmount"));
            entity.setFeeTypeName(json.getString("feeTypeName"));
            entity.setFeeTypeNo(json.getString("feeTypeNo"));
            entity.setNegotiateAmount(json.getBigDecimal("negotiateAmount"));
            entity.setPayableAmount(json.getBigDecimal("payableAmount"));
            entity.setCreateName(token.getUsername());
            chargeFeeChangeService.save(entity);
        }

        return Result.success();
    }

    @Bean(copy=true)
    //@HasPermission("pay:charge:fee:info:edit")
    @ReqMapping("/update")
    public Object update(ChargeFeeChange entity) throws Exception {

        return chargeFeeChangeService.update(entity);
    }

    //@HasPermission("pay:charge:fee:info:edit")
    @ReqMapping("/delete")
    public Object delete(ChargeFeeChange entity) {

        return chargeFeeChangeService.deleteById(entity.getId());
    }

}
